package system.cashier.impl;

import system.cashier.CashierInterface;
import system.commodityManagement.impl.Commodity;
import system.commodityManagement.impl.CommodityManagementImpl;

import java.util.Map;
import java.util.Scanner;

/**
 * Created by 陈文杰 on 2022/4/13 19:33
 */
public class CashierImpl implements CashierInterface {
    private Map<Integer, Commodity> commodityMap = null;//保存商品集合
    private StringBuilder insufficientCommodity = new StringBuilder();
    private Integer[] commodityIds;//保存购买的商品的id
    private double[] commodityPrices;//保存购买的商品的单价
    private int[] commodityNums;//保存购买的商品的数量
    private String[] commodityNames;//保存购买的商品的名称
    private double commodityPricesCount = 0.0;//保存总价格

    private Scanner scanner = new Scanner(System.in);

    public CashierImpl(CommodityManagementImpl commodityMap) {
        this.commodityMap = commodityMap.getCommodityMap();
    }

    //扫描商品
    @Override
    public void scan(String ids) {
        //为所有私有属性进行初始化
        String[] stringIds = ids.split(" ");
        commodityIds = new Integer[stringIds.length];
        commodityPrices = new double[stringIds.length];
        commodityNums = new int[stringIds.length];
        commodityNames = new String[stringIds.length];
        for (int i = 0; i < stringIds.length; i++) {//保存商品id
            commodityIds[i] = Integer.parseInt(stringIds[i]);
        }
    }

    //修改商品数量
    /*
    使用该方法前要保证commodityIds不为null
    即要先使用scan方法
     */
    @Override
    public void alterCommodityNumber() {
        String name;//暂时保存商品名
        int num = 0;//暂时保存库存数
        int buyNum = 0;//保存购买数
        //用户输入购买数量,循环次数为用户购买商品种类数
        for (int i = 0; i < commodityIds.length; i++) {
            //商品不存在
            if (!commodityMap.containsKey(commodityIds[i])) {
                System.out.println("[" + commodityIds[i] + "]不存在该商品!");
                continue;
            }
            //商品存在
            {
                name = commodityMap.get(commodityIds[i]).getName();
                //保存商品单价
                commodityPrices[i] = commodityMap.get(commodityIds[i]).getPrice();
                //保存商品名
                commodityNames[i] = name;
                System.out.print("请告诉我购买[" + name + "]的数量:");
                //保存顾客欲购买的商品数量
                commodityNums[i] = scanner.nextInt();
                //赋值:库存数减去购买数
                num = commodityMap.get(commodityIds[i]).getCount() - commodityNums[i];
                //购买商品数不多于库存数
                if (num >= 0) {
                    commodityMap.get(commodityIds[i]).setCount(num);//直接修改库存数
                }
                //购买数多于库存数
                if (num < 0) {
                    insufficientCommodity.append("商品[" + name + "]不够[" + commodityNums[i] + "] [" + commodityMap.get(commodityIds[i]).getCount() + "]都给你咯^_^\n");
                    commodityNums[i] = commodityMap.get(commodityIds[i]).getCount();//修改购买数为库存数
                    commodityMap.get(commodityIds[i]).setCount(0);//库存数直接设为零
                }
            }
        }
    }

    //结账
    @Override
    public double settleAccounts() {
        //打印顾客购买商品多于库存数的商品
        System.out.println(insufficientCommodity.toString());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //计算总额
        for (int i = 0; i < commodityPrices.length; i++) {
            commodityPricesCount += (commodityPrices[i] * commodityNums[i]);
        }
        //处理数据,将double小数点保留两位
        String str = String.format("%.2f", commodityPricesCount);
        commodityPricesCount = Double.parseDouble(str);
        return commodityPricesCount;
    }

    //打印票据
    @Override
    public void printBill() {
        System.out.println("********************好日子超市********************");
        System.out.println("商品名\t\t价格");
        for (int i = 0; i < commodityIds.length; i++) {
            if (commodityNames[i] != null) {
                System.out.println(commodityNames[i] +
                        "\t\t" + commodityPrices[i] + "*" + commodityNums[i]);
            }
        }
        System.out.println("总价:" + commodityPricesCount);
        System.out.println("********************好日子超市********************\n");
    }

    public void payment(double commodityPricesCount) {
        System.out.print("请付款:");
        double money = scanner.nextDouble();
        if (money >= commodityPricesCount) {
            String str = String.format("%.2f", money - commodityPricesCount);
            commodityPricesCount = Double.parseDouble(str);
            System.out.println("找您:" + commodityPricesCount);
        }
        if (money < commodityPricesCount) {
            System.out.println("你是故意找茬是吧,这哪够" + commodityPricesCount);
            try {
                Thread.sleep(700);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String str = String.format("%.2f", commodityPricesCount - money);
            double middle = Double.parseDouble(str);
            System.out.println("还差:" + middle);
            try {
                Thread.sleep(700);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("打钱!");
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            payment(middle);
        }

    }
}
